package org.xhy.hrm.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.xhy.hrm.asserts.GlobleAssert;
import org.xhy.hrm.client.LoginUserClient;
import org.xhy.hrm.domain.LoginUser;
import org.xhy.hrm.domain.VipBase;
import org.xhy.hrm.domain.VipUser;
import org.xhy.hrm.dto.SmsRedisDto;
import org.xhy.hrm.dto.registerDto;
import org.xhy.hrm.globle.GlobleException;
import org.xhy.hrm.mapper.VipBaseMapper;
import org.xhy.hrm.mapper.VipUserMapper;
import org.xhy.hrm.properties.ImgProperties;
import org.xhy.hrm.service.IVipUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.xhy.hrm.util.AjaxResult;
import org.xhy.hrm.util.BitStatesUtils;
import org.xhy.hrm.util.StrUtils;
import org.xhy.hrm.util.encrypt.MD5;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author xhy
 * @since 2021-09-19
 */
@Service
@Transactional
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ImgProperties properties;

    @Autowired
    private VipBaseMapper vipBaseMapper;

    @Autowired
    private VipUserMapper vipUserMapper;

    @Autowired
    private LoginUserClient loginUserClient;

    @Override
    @GlobalTransactional
    public void register(registerDto dto) {
        String mobile = dto.getMobile();
        String password = dto.getPassword();
        String smsCode = dto.getSmsCode();
        //校验参数
        GlobleAssert.hasText(mobile,"手机号不能为空");
        GlobleAssert.hasText(password,"密码不能为空");
        GlobleAssert.hasText(smsCode,"验证码不能为空");

        //判断手机号是否已经注册
        VipUser user = vipUserMapper.selectByMobile(mobile);
        if (user!=null){
            throw new GlobleException("手机号已注册，请直接登录");
        }

        //当前时间
        Date now = new Date();
        String key = properties.getSmsKey() + mobile;
        //验证手机验证码是否正确
        SmsRedisDto userInRedisDto = (SmsRedisDto) redisTemplate.opsForValue().get(key);
        if (now.getTime()-userInRedisDto.getDateTime() > 60*1000*5){
            throw new GlobleException("验证码已过期，请重新发送验证码");
        } else {
            if (!smsCode.equals(userInRedisDto.getSmsCode())){
                throw new GlobleException("验证码错误，请重新输入");
            }
        }

        //密码生成盐值
        String salt = StrUtils.getComplexRandomString(10);
        // 工具类密码加密
        String str = MD5.getMD5(password + salt);

        //保存信息
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(mobile);
        loginUser.setPassword(str);
        loginUser.setType(LoginUser.LOGIN_USER);
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);
        if (!ajaxResult.isSuccess()){
            throw new GlobleException("保存登录信息失败");
        }
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());

        //保存信息
        VipUser vipUser = new VipUser();
        vipUser.setCreateTime(new Date().getTime());
        vipUser.setPhone(mobile);
        vipUser.setSalt(salt);
        vipUser.setPassword(str);
        vipUser.setBitState(BitStatesUtils.OP_REGISTED);
        vipUser.setSecLevel(0);
        vipUser.setLoginId(loginId);
        baseMapper.insert(vipUser);

        //保存基本信息
        VipBase vipBase = new VipBase();
        vipBase.setCreateTime(new Date().getTime());
        vipBase.setSsoId(vipUser.getId());
        vipBase.setRegChannel(1);
        vipBase.setRegTime(new Date().getTime());
        vipBase.setLevel(0);
        vipBase.setGrowScore(0);
        vipBaseMapper.insert(vipBase);

        int i =1/0;

    }
}
